package com.ruoyi.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.order.utils.OrderNoUtil;
import com.ruoyi.user.domain.vo.UcenterMemberVo;
import com.ruoyi.user.mapper.UcenterMemberMapper;
import com.ruoyi.user.service.IUcenterMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.order.domain.bo.TOrderBo;
import com.ruoyi.order.domain.vo.TOrderVo;
import com.ruoyi.order.domain.TOrder;
import com.ruoyi.order.mapper.TOrderMapper;
import com.ruoyi.order.service.ITOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-29
 */
@RequiredArgsConstructor
@Service
public class TOrderServiceImpl implements ITOrderService {

    private final TOrderMapper baseMapper;
    private final UcenterMemberMapper ucenterMemberMapper;
    private final EduCourseMapper eduCourseMapper;

    /**
     * 查询订单
     */
    @Override
    public TOrderVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单列表
     */
    @Override
    public TableDataInfo<TOrderVo> queryPageList(TOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TOrder> lqw = buildQueryWrapper(bo);
        Page<TOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单列表
     */
    @Override
    public List<TOrderVo> queryList(TOrderBo bo) {
        LambdaQueryWrapper<TOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TOrder> buildQueryWrapper(TOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TOrder> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOrderNo()), TOrder::getOrderNo, bo.getOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getCourseTitle()), TOrder::getCourseTitle, bo.getCourseTitle());
        lqw.like(StringUtils.isNotBlank(bo.getTeacherName()), TOrder::getTeacherName, bo.getTeacherName());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), TOrder::getNickname, bo.getNickname());
        lqw.like(StringUtils.isNotBlank(bo.getMobile()), TOrder::getMobile, bo.getMobile());
        lqw.eq(bo.getPayType() != null, TOrder::getPayType, bo.getPayType());
        lqw.eq(bo.getStatus() != null, TOrder::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增订单
     */
    @Override
    public Boolean insertByBo(TOrderBo bo) {
        TOrder add = BeanUtil.toBean(bo, TOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单
     */
    @Override
    public Boolean updateByBo(TOrderBo bo) {
        TOrder update = BeanUtil.toBean(bo, TOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean createOrder(String courseId, String memberIdByJwtToken) throws Exception {
//        通过远程调用根据用户id获取用户信息
        UcenterMemberVo ucenterMemberVo = ucenterMemberMapper.selectVoById(memberIdByJwtToken);//        通过远程调用根据用户id获取用户信息
//        通过远程调用根据课程id获取课程信息
        EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(courseId);
        Long payType = Long.valueOf(1);
        TOrderBo tOrderBo = new TOrderBo();
        tOrderBo.setOrderNo(OrderNoUtil.getOrderNo());
        tOrderBo.setCourseId(courseId);
        tOrderBo.setCourseTitle(eduCourseVo.getTitle());
        tOrderBo.setCourseCover(eduCourseVo.getCover());
        tOrderBo.setTeacherName(eduCourseVo.getName());
        tOrderBo.setMemberId(memberIdByJwtToken);
        tOrderBo.setNickname(ucenterMemberVo.getNickname());
        tOrderBo.setMobile(ucenterMemberVo.getMobile());
        tOrderBo.setTotalFee(eduCourseVo.getPrice());
        tOrderBo.setPayType(payType);
        if(tOrderBo.getOrderNo()== null  || tOrderBo.getCourseId()!= null || tOrderBo.getMemberId()!=null){
            throw new Exception("订单生成失败");
        }
       return true;
    }
}
